<?php

namespace addons\csmitsm\library\xcore\xcflow\utils;

use addons\csmitsm\library\sdnode\xstack\utils\XsJsonUtils;
use addons\csmitsm\library\xcore\xcore\XcException;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDatabaseUtils;
use addons\csmitsm\library\xcore\xcflow\utils\XcformFieldsUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcAdminSessionUtils;

class XcformPageUtils
{
    public static function getViewsByPageid($page_id)
    {
        return XcDaoUtils::getListBySql(new \app\admin\model\csmitsm\Xcflowviewpageview, [
            ["xcflowviewpage_id", "=", $page_id]
        ], null, "weigh asc");
    }

    // if view_id null then first view
    public static function getViewIfNullThenFirstView($page_id, $view_id)
    {
        $view = null;

        $dao = new \app\admin\model\csmitsm\Xcflowviewpageview();
        if (!empty($view_id)) {
            $view = XcDaoUtils::getRowById($dao, $view_id);
        } else {
            $view = XcDaoUtils::getRowBySql(new \app\admin\model\csmitsm\Xcflowviewpageview(), [
                ['xcflowviewpage_id', '=', $page_id]
            ], null, true, false, 'weigh asc');
        }

        if ($view) {
            // $view['configjsonObj'] = json_decode($view->configjson, true);
            $view['configjsonObj'] = XsJsonUtils::stringToJson($view->configjson);
            $view_id = $view->id;
        }

        return $view;
    }


    public static function getXcfieldjsonObjByTablename($tablename)
    {
        $xcallform = XcflowallformUtils::getTableinfoByTablename($tablename);

        $xcfieldjsonObj = [];
        if ($xcallform['xcformtype'] == 'view') {
            // find xcform&xcformmdm, convert to xcfieldjson from fa_csmitsm_xcflowform's table_xcfieldjson
            // $fieldjsonObj = json_decode($xcallform['view_fieldjson'], true);
            $fieldjsonObj = XsJsonUtils::stringToJson($xcallform['view_fieldjson']);
            foreach ($fieldjsonObj as $item) {
                $tableinfo = XcDatabaseUtils::getTableInfo($item['tablename']);
                $tt = null;
                switch ($item['tablecolumn']) {
                    case "id":
                        $tt = [
                            'title' => $tableinfo['comment'],
                            'fieldjson' => '{"xcform_base_selecttablefield":{"tablename":"' . $item['tablename'] . '"}}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_objselect',
                        ];
                        break;
                    case "admin_id":
                        $tt = [
                            'title' => $tableinfo['comment'] . 'admin_id',
                            'fieldjson' => '{"xcform_base_selecttablefield":{"tablename":"' . XcDatabaseUtils::appendTablePrefix("admin") . '"}}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_objselect',
                        ];
                        break;
                    case "user_id":
                        $tt = [
                            'title' => $tableinfo['comment'] . 'user_id',
                            'fieldjson' => '{"xcform_base_selecttablefield":{"tablename":"' . XcDatabaseUtils::appendTablePrefix("user") . '"}}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_objselect',
                        ];
                        break;
                    case "createtime":
                        $tt = [
                            'title' => $tableinfo['comment'] . '创建时间',
                            'fieldjson' => '{}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_input_datetime',
                        ];
                        break;
                    case "updatetime":
                        $tt = [
                            'title' => $tableinfo['comment'] . '修改时间',
                            'fieldjson' => '{}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_input_datetime',
                        ];
                        break;
                    case "status":
                        $tt = [
                            'title' => $tableinfo['comment'] . '数据状态',
                            'fieldjson' => '{}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_input_string',
                        ];
                        break;
                    case "weigh":
                        $tt = [
                            'title' => $tableinfo['comment'] . '排序',
                            'fieldjson' => '{}',
                            'fieldcode' => $item['column'],
                            'fieldtag' => 'xcform_control_input_string',
                        ];
                        break;
                    default:
                        $targetTable = XcflowallformUtils::getTableinfoByTablename($item['tablename']);
                        // $targetXcfieldjsonObj = json_decode($targetTable['table_xcfieldjson'], true);
                        $targetXcfieldjsonObj = XsJsonUtils::stringToJson($targetTable['table_xcfieldjson']);

                        foreach ($targetXcfieldjsonObj as $targetXcfield) {
                            if ($targetXcfield['fieldcode'] == $item['tablecolumn']) {
                                $targetXcfield['fieldcode'] = $item['column'];
                                $tt = $targetXcfield;
                            }
                        }
                }
                if (!$tt || !isset($tt['fieldjson'])) {
                    throw new XcException("流程字段配置错误", 0, null, $item['tablename'] . "缺少缺少" . $item['tablecolumn'] . "的xcfieldjson配置");
                }

                // $tt['fieldjsonObj'] = json_decode($tt['fieldjson'], true);
                $tt['fieldjsonObj'] = XsJsonUtils::stringToJson($tt['fieldjson']);
                $xcfieldjsonObj[] = $tt;
            }
        } else {
            $xcform = XcflowallformUtils::getXcformByRow($xcallform);
            $xcfieldjsonObj = XcformFieldsUtils::getXcfieldjsonJsonObjectFromXcform($xcform);
            // $xcfieldjsonObj = json_decode($xcallform['table_xcfieldjson'], true);
        }

        return $xcfieldjsonObj;
    }

    public static function processFiltersql($filtersql)
    {
        if ($filtersql != null) {
            $filtersql = str_replace("'", "‘", $filtersql);
            $filtersql = str_replace("--", "－－", $filtersql);
            $filtersql = str_replace('[[sessionadminid]]', XcAdminSessionUtils::getUserId(), $filtersql);

            $roleids = join(',', XcAdminSessionUtils::getAuthGroupIds());
            $filtersql = str_replace('[[sessionroleids]]', "({$roleids})", $filtersql);
        }
        return $filtersql;
    }
}
